package com.huan.job;

import com.huan.domain.entity.Article;
import com.huan.mapper.ArticleMapper;
import com.huan.service.ArticleService;
import com.huan.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ArticleViewCountJob {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisCache redisCache;

    @Scheduled(cron = "0/6 * * * * ?")
    public void updateArticle(){
        Map<String, Integer> viewCount = redisCache.getCacheMap("ArticleViewCount");
        List<Article> list = viewCount.entrySet().stream()
                .map(entry -> new Article(Long.valueOf(entry.getKey()), entry.getValue().longValue()))
                .collect(Collectors.toList());

        articleService.updateBatchById(list);
    }


















}
